package com.small.controller.backend;

import com.small.common.Const;
import com.small.common.ServerResponse;
import com.small.pojo.User;
import com.small.service.IUserService;
import com.small.util.CookieUtil;
import com.small.util.JsonUtil;
import com.small.util.RedisShradedPoolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @program: small
 * @description:管理员账户登录
 * @author: Mr.Xie
 * @create: 2019-04-23 16:43
 */
@Controller
@RequestMapping("/manage/user")
public class UserManageController {

    @Autowired
    private IUserService iUserService;
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> userlogin(String username, String password, HttpSession session, HttpServletResponse httpresponse){
        ServerResponse<User> response = iUserService.login(username, password);
        if(response.isSuccess()){
            User user=response.getData();
            if(user.getRole()== Const.Role.ROLE_ADMIN){
                //说明是管理员登录
//                session.setAttribute(Const.CURRENT_USER,user);
                CookieUtil.writeLoginToken(httpresponse,session.getId());
                RedisShradedPoolUtil.setEx(session.getId(), JsonUtil.obj2String(response.getData()),Const.RedisCacheExtime.REDIS_SESSION_EXTIME);
                return response;
            }else {
                return ServerResponse.createByErrorMessage("不是管理员无法登陆");
            }

        }
        return response;
    }

}
